package com.hackerrank.challenges.billboards;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Solution {
	private static class BalancedTree<E extends Comparable<E>> {
		Node<E> root;
		private static class Node<E extends Comparable<E>> {
			private E e;
			private int h = 1;
			private Node<E> l, r;
			public Node(E v) {
				this.e = v;
			}
			private int balanceFactor() {
				return (l == null ? 0 : l.h) - (r == null ? 0 : r.h);
			}
			private void recalculate() {
				h = Math.max((l == null ? 0 : l.h), (r == null ? 0 : r.h)) + 1;
			}
			public Node<E> add(E n) {
				if(n.compareTo(e) >= 0) {
					if(r == null)
						r = new Node<E>(n);
					else
						r = r.add(n);
				} else if(n.compareTo(e) < 0) {
					if(l == null)
						l = new Node<E>(n);
					else
						l = l.add(n);
				}
				return balance();
			}
			public Node<E> remove(E n) {
				if(n.compareTo(e) < 0)
					l = l.remove(n);
				else if(n.compareTo(e) > 0)
					r = r.remove(n);
				else {
					if(r == null && l == null)
						return null;
					int rh = r == null ? 0 : r.h, lh = l == null ? 0 : l.h;
					if(rh >= lh) {
						e = r.getMin();
						r = r.remove(e);
					} else if(rh < lh) {
						e = l.getMax();
						l = l.remove(e);
					}
				}
				return balance();
			}
			private E getMin() {
				Node<E> node = this;
				while(node.l != null)
					node = node.l;
				return node.e;
			}
			private E getMax() {
				Node<E> node = this;
				while(node.r != null)
					node = node.r;
				return node.e;
			}
			private Node<E> balance() {
				recalculate();
				if(balanceFactor() < -1) {
					int rlh = r.l == null ? 0 : r.l.h, rrh = r.r == null ? 0 : r.r.h;
					if (rlh > rrh) {
						// this is the first step in the right-left case.
						Node<E> tr = r, trl = r.l, C = trl.r;
						tr.l = C;
						tr.recalculate();
						trl.r = tr;
						trl.recalculate();
						r = trl;
						recalculate();
					}
					// This brings us to the right-right case.
					Node<E> tr = r, B = tr.l;
					r = B;
					recalculate();
					tr.l = this;
					tr.recalculate();
					return tr;
				}
				if(balanceFactor() > 1) {
					int llh = l.l == null ? 0 : l.l.h, lrh = l.r == null ? 0 : l.r.h;
					if (lrh > llh) {
						// this is the first step in the left-right case.
						Node<E> tl = l, tlr = l.r, B = tlr.l;
						tl.r = B;
						tl.recalculate();
						tlr.l = tl;
						tlr.recalculate();
						l = tlr;
						recalculate();
					}
					// This brings us to the left-left case.
					Node<E> tl = l, C = tl.r;
					l = C;
					recalculate();
					tl.r = this;
					tl.recalculate();
					return tl;
				}
				return this;
			}
			public String toString() {
				if(l == null && r == null)
					return e.toString();
				return "{" + l + ", " + e + ", " + r + "}";
			}
		}
		public void add(E n) {
			if(root == null)
				root = new Node<E>(n);
			else
				root = root.add(n);
		}
		public void remove(E n) {
			if(root == null)
				return;
			root = root.remove(n);
		}
		public E getMin() {
			if(root == null)
				return null;
			return root.getMin();
		}
		public String toString() {
			if(root == null)
				return null;
			return root.toString();
		}
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt(), K = in.nextInt();
		BalancedTree<Long> tree = new BalancedTree<Long>();
		Queue<Long> queue = new LinkedList<Long>();
		long total = 0;
		for(int i = 0; i <= K; i++) {
			long p = in.nextLong();
			total += p;
			queue.add(p);
			tree.add(p);
		}
		for(int i = K + 1; i < N; i++) {
			long p = in.nextLong();
			total += p;
			long np = tree.getMin() + p;
			queue.add(np);
			tree.add(np);
			tree.remove(queue.remove());
		}
		System.out.println(total - tree.getMin());
	}
}
